package com.itqf.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itqf.entity.SysSensitiveWords;
import com.itqf.mapper.SensitiveWordMapper;
import com.itqf.service.SensitiveWordService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2021/1/9
 * @Time: 上午11:36
 */
@Service(version = "3.0")  //com.alibaba.dubbo.config.annotation.Service
public class SensitiveWordServiceImpl implements SensitiveWordService {

    @Resource
    private SensitiveWordMapper sensitiveWordMapper;


    /**
     * 单词是否是敏感词
     * @param word
     * @return
     */
    @Override
    public boolean isHarmonious(String word) {
        List list = findHarmonious(word);
        if (list!=null&&list.size()>0){
            return  true;
        }
        return false;
    }

    @Override
    public String toHarmonious(String word) {
        //傻逼，推搭呀,tmd！  ---> **，推搭呀！***
        //diff算法
        List<SysSensitiveWords> list = sensitiveWordMapper.selectList(null);
        //String s = null;
        if (list!=null&&list.size()>0){
            for (int i = 0; i < list.size(); i++) {
              String  sensitiveWord = list.get(i).getSensitiveWord();

               word = word.replaceAll(sensitiveWord,getNew(sensitiveWord.length()));
            }
        }


        return word;
    }

    public  String   getNew(int len){
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < len; i++) {
            sb.append("*");
        }

        return  sb.toString();
    }

    @Override
    public boolean addHarmonious(SysSensitiveWords sysSensitiveWords) {
       int i = sensitiveWordMapper.insert(sysSensitiveWords);

        return i>0;
    }

    @Override
    public boolean delHarmonious(String word) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("sensitive_word",word);

        int i = sensitiveWordMapper.delete(queryWrapper);

        return i>0;
    }

    @Override
    public List<SysSensitiveWords> findHarmonious(String word) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("sensitive_word",word);
        List<SysSensitiveWords> list   = sensitiveWordMapper.selectList(queryWrapper);

        return list;
    }
}
